<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateLawsuitLegalOrders extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        // 创建诉讼订单表
        $this->createLawsuitOrdersTable();
        
        // 创建法务服务订单表
        $this->createLegalServiceOrdersTable();
    }
    
    /**
     * 创建诉讼订单表
     */
    protected function createLawsuitOrdersTable()
    {
        $table = $this->table('crm_lawsuit_orders', ['engine' => 'InnoDB', 'comment' => '诉讼订单表']);
        $table->addColumn('order_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '关联订单ID'])
            ->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '客户ID'])
            ->addColumn('is_member', 'tinyint', ['limit' => 1, 'default' => 0, 'comment' => '是否会员: 0-否 1-是'])
            ->addColumn('party_type', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '委托方类型: 1-企业 2-个人'])
            ->addColumn('party_name', 'string', ['limit' => 255, 'default' => '', 'comment' => '委托方名称'])
            ->addColumn('opposite_name', 'string', ['limit' => 255, 'default' => '', 'comment' => '对方当事人'])
            ->addColumn('case_reason', 'string', ['limit' => 255, 'default' => '', 'comment' => '案由'])
            ->addColumn('case_type', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '案件类型: 1-民商 2-刑事 3-行政 4-执行 5-劳动 6-其他'])
            ->addColumn('stage', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '诉讼阶段: 1-起诉(一审) 2-二审 3-再审 4-执行 5-仲裁'])
            ->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '标的金额'])
            ->addColumn('amount_range', 'tinyint', ['limit' => 1, 'default' => 0, 'comment' => '标的金额范围'])
            ->addColumn('court_name', 'string', ['limit' => 255, 'default' => '', 'comment' => '法院名称'])
            ->addColumn('agent_scope', 'string', ['limit' => 500, 'default' => '', 'comment' => '代理范围'])
            ->addColumn('cost_type', 'string', ['limit' => 255, 'default' => '', 'comment' => '诉讼收费类型'])
            ->addColumn('main_requests', 'text', ['comment' => '主要诉讼请求'])
            ->addColumn('customer_needs', 'text', ['comment' => '客户需求'])
            ->addColumn('project_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '项目经理'])
            ->addColumn('department_head', 'string', ['limit' => 50, 'default' => '', 'comment' => '部门主管'])
            ->addColumn('legal_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '法务经理'])
            ->addColumn('company_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '公司管理层'])
            ->addColumn('status', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '状态: 1-进行中 2-已完成 3-已取消'])
            ->addColumn('create_time', 'datetime', ['default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => null, 'comment' => '更新时间'])
            ->addColumn('delete_time', 'datetime', ['default' => null, 'comment' => '删除时间'])
            ->addIndex(['order_id'], ['name' => 'idx_order_id'])
            ->addIndex(['customer_id'], ['name' => 'idx_customer_id'])
            ->create();
    }
    
    /**
     * 创建法务服务订单表
     */
    protected function createLegalServiceOrdersTable()
    {
        $table = $this->table('crm_legal_service_orders', ['engine' => 'InnoDB', 'comment' => '法务服务订单表']);
        $table->addColumn('order_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '关联订单ID'])
            ->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '客户ID'])
            ->addColumn('service_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '服务费用'])
            ->addColumn('order_type', 'tinyint', ['limit' => 1, 'default' => 2, 'comment' => '套餐类型: 1-按次收费 2-年服务费 3-VIP服务'])
            ->addColumn('license_type', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '资质类型: 1-主板上市 2-新三板'])
            ->addColumn('service_count', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '服务数量'])
            ->addColumn('contract_term', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '合同期限: 1-半年 2-一年 3-两年 4-三年'])
            ->addColumn('service_content', 'text', ['comment' => '服务内容'])
            ->addColumn('legal_advisor', 'string', ['limit' => 50, 'default' => '', 'comment' => '法律顾问'])
            ->addColumn('contract_status', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '合同状态: 1-未签署 2-已签署 3-已到期 4-已终止'])
            ->addColumn('business_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '商务经理'])
            ->addColumn('department_head', 'string', ['limit' => 50, 'default' => '', 'comment' => '部门主管'])
            ->addColumn('legal_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '法务经理'])
            ->addColumn('company_manager', 'string', ['limit' => 50, 'default' => '', 'comment' => '公司管理层'])
            ->addColumn('status', 'tinyint', ['limit' => 1, 'default' => 1, 'comment' => '状态: 1-进行中 2-已完成 3-已取消'])
            ->addColumn('create_time', 'datetime', ['default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => null, 'comment' => '更新时间'])
            ->addColumn('delete_time', 'datetime', ['default' => null, 'comment' => '删除时间'])
            ->addIndex(['order_id'], ['name' => 'idx_order_id'])
            ->addIndex(['customer_id'], ['name' => 'idx_customer_id'])
            ->create();
    }
}